package com.sendi.system.service;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;

import org.hibernate.Session;

import com.sendi.system.entity.SystemPower;
import com.sendi.system.entity.UserRole;
import com.sendi.system.entity.UserRoleRelation;

/*
 * 用户角色的运行类
 * 缓存到内存中，做为单例运行
 */
public class UserRoleService {
	
	public ServiceSupportImp serviceSupportImp;
    /*
     * 角色ID,对应角色名称
     */
	public HashMap<String, String> userroleidtoname=new HashMap<String, String>();
	public HashMap<String, UserRole> userroleidtouserole=new HashMap<String, UserRole>();
	/*
	 *用户ID，对应的角色ID ,角色ID使用逗号分隔
	 */
	public HashMap<String, String> userid2roleid=new HashMap<String, String>();
	
	/*
	 *角色ID 对应的 用户ID 用户ID使用逗号分隔
	 */
	public  HashMap<String, String> roleid2userid=new HashMap<String, String>();
	
   private HashMap<String,HashSet<String>> moudleid2roleid=new HashMap<String, HashSet<String>>();

	public HashMap<String, String> getUserroleidtoname() {
		return userroleidtoname;
	}

	public void setUserroleidtoname(HashMap<String, String> userroleidtoname) {
		this.userroleidtoname = userroleidtoname;
	}
	
	/*
	 * 初始化方法
	 */
	public void init()
	{
		Session session=this.serviceSupportImp.getDao().getSessionFactory().openSession();
		String hql="from UserRole";
        List datas=session.createQuery(hql).list();
        for(Object o:datas)
        {
        	UserRole ur=(UserRole)o;
        	userroleidtoname.put(ur.getId()+"", ur.getRolename());
        	userroleidtouserole.put(ur.getId()+"", ur);
        	
        }
        
        hql="from UserRoleRelation";
          datas=session.createQuery(hql).list();
        for(Object o:datas)
        {
        	UserRoleRelation ur=(UserRoleRelation)o;
        	if(userid2roleid.get(ur.getUserid()+"")==null)
        	{
        		userid2roleid.put(ur.getUserid()+"", ur.getRoleid()+"");
        	}
        	else
        	{
        		String roleid=userid2roleid.get(ur.getUserid()+"");
        		userid2roleid.put(ur.getUserid()+"", ur.getRoleid()+","+roleid);
        	}
        	
        	if(roleid2userid.get(ur.getRoleid()+"")==null)
        	{
        		roleid2userid.put(ur.getRoleid()+"", ur.getUserid()+"");
        	}
        	else
        	{
        		String userids=roleid2userid.get(ur.getRoleid()+"");
        		roleid2userid.put(ur.getRoleid()+"", ur.getUserid()+","+userids);
        	}
        	
         
        	
        }
         String hql7="select  o.roleid,k.moudleid from SystemPower o,Functionmoudle k  where o.moudleid =k.id";
	   	 List datas7=session.createQuery(hql7).list();
	  	 for(Object o7:datas7)
		 {
			 Object kk[] =(Object[])o7;
		 	 HashSet<String> tt=moudleid2roleid.get(kk[1]);
		 	 if(tt==null)
			 {
				 tt=new HashSet<String>();
			 	 tt.add(String.valueOf((Integer)kk[0]));
				 moudleid2roleid.put(kk[1]+"", tt);
				 
			 }
			  tt.add(String.valueOf(kk[0]));
	 	 }
		 session.close();
 	 
	}

	public ServiceSupportImp getServiceSupportImp() {
		return serviceSupportImp;
	}

	public void setServiceSupportImp(ServiceSupportImp serviceSupportImp) {
		this.serviceSupportImp = serviceSupportImp;
	}

	public HashMap<String, UserRole> getUserroleidtouserole() {
		return userroleidtouserole;
	}

	public void setUserroleidtouserole(HashMap<String, UserRole> userroleidtouserole) {
		this.userroleidtouserole = userroleidtouserole;
	}

	public HashMap<String, String> getUserid2roleid() {
		return userid2roleid;
	}

	public void setUserid2roleid(HashMap<String, String> userid2roleid) {
		this.userid2roleid = userid2roleid;
	}

	public HashMap<String, String> getRoleid2userid() {
		return roleid2userid;
	}

	public void setRoleid2userid(HashMap<String, String> roleid2userid) {
		this.roleid2userid = roleid2userid;
	}

	public HashMap<String, HashSet<String>> getMoudleid2roleid() {
		return moudleid2roleid;
	}
	
	
}
